package log;

import config.Config;
import utils.TimeUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Logger {
    private static FileOutputStream fileOutputStream = null;

    public static void openFile() {
        String webRoot = Config.getConfig().getWebRoot();
        String logFilename = webRoot + "/log/" + TimeUtils.now() + ".txt";

        File f = new File(logFilename);
        try {
            boolean ret = f.createNewFile();
            fileOutputStream = new FileOutputStream(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static synchronized void log(LogInfo info) {
        if (fileOutputStream == null) {
            openFile();
        }

        try {
            fileOutputStream.write(info.toString().getBytes());
            fileOutputStream.write("\n".getBytes());
            fileOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
